<!doctype html>
<html lang="en">
<head>
  <meta charset='utf-8'>
  <title>tuyapi 7.2.0 | Documentation</title>
  <meta name='description' content='An easy-to-use API for devices that use Tuya&#39;s cloud services'>
  <meta name='viewport' content='width=device-width,initial-scale=1'>
  <link href='assets/bass.css' rel='stylesheet'>
  <link href='assets/style.css' rel='stylesheet'>
  <link href='assets/github.css' rel='stylesheet'>
  <link href='assets/split.css' rel='stylesheet'>
</head>
<body class='documentation m0'>
    <div class='flex'>
      <div id='split-left' class='overflow-auto fs0 height-viewport-100'>
        <div class='py1 px2'>
          <h3 class='mb0 no-anchor'>tuyapi</h3>
          <div class='mb1'><code>7.2.0</code></div>
          <input
            placeholder='Filter'
            id='filter-input'
            class='col12 block input'
            spellcheck='false'
            autocapitalize='off'
            autocorrect='off'
            type='text' />
          <div id='toc'>
            <ul class='list-reset h5 py1-ul'>
              
                
                <li><a
                  href='#tuyadevice'
                  class=" toggle-sibling">
                  TuyaDevice
                  <span class='icon'>▸</span>
                </a>
                
                <div class='toggle-target display-none'>
                  
                  
                    <ul class='list-reset py1-ul pl1'>
                      <li class='h5'><span>Instance members</span></li>
                      
                      <li><a
                        href='#tuyadeviceget'
                        class='regular pre-open'>
                        #get
                      </a></li>
                      
                      <li><a
                        href='#tuyadevicerefresh'
                        class='regular pre-open'>
                        #refresh
                      </a></li>
                      
                      <li><a
                        href='#tuyadeviceset'
                        class='regular pre-open'>
                        #set
                      </a></li>
                      
                      <li><a
                        href='#tuyadeviceconnect'
                        class='regular pre-open'>
                        #connect
                      </a></li>
                      
                      <li><a
                        href='#tuyadevicedisconnect'
                        class='regular pre-open'>
                        #disconnect
                      </a></li>
                      
                      <li><a
                        href='#tuyadeviceisconnected'
                        class='regular pre-open'>
                        #isConnected
                      </a></li>
                      
                      <li><a
                        href='#tuyadeviceresolveid'
                        class='regular pre-open'>
                        #resolveId
                      </a></li>
                      
                      <li><a
                        href='#tuyadevicefind'
                        class='regular pre-open'>
                        #find
                      </a></li>
                      
                      <li><a
                        href='#tuyadevicetoggle'
                        class='regular pre-open'>
                        #toggle
                      </a></li>
                      
                    </ul>
                  
                  
                  
                </div>
                
                </li>
              
                
                <li><a
                  href='#events'
                  class="h5 bold black caps">
                  Events
                  
                </a>
                
                </li>
              
                
                <li><a
                  href='#eventtuyadeviceerror'
                  class="">
                  TuyaDevice#error
                  
                </a>
                
                </li>
              
                
                <li><a
                  href='#eventtuyadeviceconnected'
                  class="">
                  TuyaDevice#connected
                  
                </a>
                
                </li>
              
                
                <li><a
                  href='#eventtuyadeviceheartbeat'
                  class="">
                  TuyaDevice#heartbeat
                  
                </a>
                
                </li>
              
                
                <li><a
                  href='#eventtuyadevicedp-refresh'
                  class="">
                  TuyaDevice#dp-refresh
                  
                </a>
                
                </li>
              
                
                <li><a
                  href='#eventtuyadevicedata'
                  class="">
                  TuyaDevice#data
                  
                </a>
                
                </li>
              
                
                <li><a
                  href='#eventtuyadevicedisconnected'
                  class="">
                  TuyaDevice#disconnected
                  
                </a>
                
                </li>
              
                
                <li><a
                  href='#packet'
                  class="">
                  Packet
                  
                </a>
                
                </li>
              
                
                <li><a
                  href='#messageparser'
                  class=" toggle-sibling">
                  MessageParser
                  <span class='icon'>▸</span>
                </a>
                
                <div class='toggle-target display-none'>
                  
                  
                    <ul class='list-reset py1-ul pl1'>
                      <li class='h5'><span>Instance members</span></li>
                      
                      <li><a
                        href='#messageparserparsepacket'
                        class='regular pre-open'>
                        #parsePacket
                      </a></li>
                      
                      <li><a
                        href='#messageparsergetpayload'
                        class='regular pre-open'>
                        #getPayload
                      </a></li>
                      
                      <li><a
                        href='#messageparserparse'
                        class='regular pre-open'>
                        #parse
                      </a></li>
                      
                      <li><a
                        href='#messageparserencode'
                        class='regular pre-open'>
                        #encode
                      </a></li>
                      
                    </ul>
                  
                  
                  
                </div>
                
                </li>
              
                
                <li><a
                  href='#tuyacipher'
                  class=" toggle-sibling">
                  TuyaCipher
                  <span class='icon'>▸</span>
                </a>
                
                <div class='toggle-target display-none'>
                  
                  
                    <ul class='list-reset py1-ul pl1'>
                      <li class='h5'><span>Instance members</span></li>
                      
                      <li><a
                        href='#tuyacipherencrypt'
                        class='regular pre-open'>
                        #encrypt
                      </a></li>
                      
                      <li><a
                        href='#tuyacipherdecrypt'
                        class='regular pre-open'>
                        #decrypt
                      </a></li>
                      
                      <li><a
                        href='#tuyaciphermd5'
                        class='regular pre-open'>
                        #md5
                      </a></li>
                      
                    </ul>
                  
                  
                  
                </div>
                
                </li>
              
                
                <li><a
                  href='#crc32'
                  class="">
                  crc32
                  
                </a>
                
                </li>
              
            </ul>
          </div>
          <div class='mt1 h6 quiet'>
            <a href='https://documentation.js.org/reading-documentation.html'>Need help reading this?</a>
          </div>
        </div>
      </div>
      <div id='split-right' class='relative overflow-auto height-viewport-100'>
        
          
          <section class='p2 mb2 clearfix bg-white minishadow'>

  
  <div class='clearfix'>
    
    <h3 class='fl m0' id='tuyadevice'>
      TuyaDevice
    </h3>
    
    
  </div>
  

  <p>Represents a Tuya device.</p>
<p>You <em>must</em> pass either an IP or an ID. If
you're experiencing problems when only passing
one, try passing both if possible.</p>

    <div class='pre p1 fill-light mt0'>new TuyaDevice(options: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)</div>
  
  
    <p>
      Extends
      
        EventEmitter
      
    </p>
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>options</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>
            = <code>{}</code>)</code>
	    
          </div>
          
          <table class='mt1 mb2 fixed-table h5 col-12'>
            <colgroup>
              <col width='30%' />
              <col width='70%' />
            </colgroup>
            <thead>
              <tr class='bold fill-light'>
                <th>Name</th>
                <th>Description</th>
              </tr>
            </thead>
            <tbody class='mt1'>
              
                <tr>
  <td class='break-word'><span class='code bold'>options.ip</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>?</code>
  </td>
  <td class='break-word'><span>IP of device
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.port</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></code>
  
    (default <code>6668</code>)
  </td>
  <td class='break-word'><span>port of device
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.id</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>?</code>
  </td>
  <td class='break-word'><span>ID of device (also called 
<code>devId</code>
)
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.gwID</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a></code>
  
    (default <code>&#39;&#39;</code>)
  </td>
  <td class='break-word'><span>gateway ID (not needed for most devices),
if omitted assumed to be the same as 
<code>options.id</code>
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.key</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a></code>
  </td>
  <td class='break-word'><span>encryption key of device (also called 
<code>localKey</code>
)
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.productKey</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>?</code>
  </td>
  <td class='break-word'><span>product key of device (currently unused)
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.version</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></code>
  
    (default <code>3.1</code>)
  </td>
  <td class='break-word'><span>protocol version
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.nullPayloadOnJSONError</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>
  
    (default <code>false</code>)
  </td>
  <td class='break-word'><span>if true, emits a data event
containing a payload of null values for on-device JSON parsing errors
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.issueGetOnConnect</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>
  
    (default <code>true</code>)
  </td>
  <td class='break-word'><span>if true, sends GET request after
connection is established. This should probably be 
<code>false</code>
 in synchronous usage.
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.issueRefreshOnConnect</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>
  
    (default <code>false</code>)
  </td>
  <td class='break-word'><span>if true, sends DP_REFRESH request after
connection is established. This should probably be 
<code>false</code>
 in synchronous usage.
</span></td>
</tr>

		                    <tr>
  <td class='break-word'><span class='code bold'>options.issueRefreshOnPing</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>
  
    (default <code>false</code>)
  </td>
  <td class='break-word'><span>if true, sends DP_REFRESH and GET request after
every ping. This should probably be 
<code>false</code>
 in synchronous usage.
</span></td>
</tr>

              
            </tbody>
          </table>
          
        </div>
      
    </div>
  

  

  

  

  

  
    <div class='py1 quiet mt1 prose-big'>Example</div>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-keyword">const</span> tuya = <span class="hljs-keyword">new</span> TuyaDevice({<span class="hljs-attr">id</span>: <span class="hljs-string">'xxxxxxxxxxxxxxxxxxxx'</span>,
                             <span class="hljs-attr">key</span>: <span class="hljs-string">'xxxxxxxxxxxxxxxx'</span>})</pre>
    
  

  

  
    <div class='py1 quiet mt1 prose-big'>Instance Members</div>
    <div class="clearfix">
  
    <div class='border-bottom' id='tuyadeviceget'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>get(options = {})</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Gets a device's current status.
Defaults to returning only the value of the first DPS index.</p>

    <div class='pre p1 fill-light mt0'>get(options: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>?): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)></div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>options</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>?
            = <code>{}</code>)</code>
	    
          </div>
          
          <table class='mt1 mb2 fixed-table h5 col-12'>
            <colgroup>
              <col width='30%' />
              <col width='70%' />
            </colgroup>
            <thead>
              <tr class='bold fill-light'>
                <th>Name</th>
                <th>Description</th>
              </tr>
            </thead>
            <tbody class='mt1'>
              
                <tr>
  <td class='break-word'><span class='code bold'>options.schema</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>?</code>
  </td>
  <td class='break-word'><span>true to return entire list of properties from device
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.dps</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></code>
  
    (default <code>1</code>)
  </td>
  <td class='break-word'><span>DPS index to return
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.cid</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>?</code>
  </td>
  <td class='break-word'><span>if specified, use device id of zigbee gateway and cid of subdevice to get its status
</span></td>
</tr>


              
            </tbody>
          </table>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)></code>:
        returns boolean if single property is requested, otherwise returns object of results

      
    
  

  

  

  
    <div class='py1 quiet mt1 prose-big'>Example</div>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-comment">// get first, default property from device</span>
tuya.get().then(<span class="hljs-function"><span class="hljs-params">status</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(status))</pre>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-comment">// get second property from device</span>
tuya.get({<span class="hljs-attr">dps</span>: <span class="hljs-number">2</span>}).then(<span class="hljs-function"><span class="hljs-params">status</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(status))</pre>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-comment">// get all available data from device</span>
tuya.get({<span class="hljs-attr">schema</span>: <span class="hljs-literal">true</span>}).then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(data))</pre>
    
  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='tuyadevicerefresh'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>refresh(options = {})</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Refresh a device's current status.
Defaults to returning all values.</p>

    <div class='pre p1 fill-light mt0'>refresh(options: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>?): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>></div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>options</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>?
            = <code>{}</code>)</code>
	    
          </div>
          
          <table class='mt1 mb2 fixed-table h5 col-12'>
            <colgroup>
              <col width='30%' />
              <col width='70%' />
            </colgroup>
            <thead>
              <tr class='bold fill-light'>
                <th>Name</th>
                <th>Description</th>
              </tr>
            </thead>
            <tbody class='mt1'>
              
                <tr>
  <td class='break-word'><span class='code bold'>options.schema</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>?</code>
  </td>
  <td class='break-word'><span>true to return entire list of properties from device
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.dps</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></code>
  
    (default <code>1</code>)
  </td>
  <td class='break-word'><span>DPS index to return
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.cid</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>?</code>
  </td>
  <td class='break-word'><span>if specified, use device id of zigbee gateway and cid of subdevice to refresh its status
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.requestedDPS</span> <code class='quiet'>Array.Number</code>
  
    (default <code>[4,5,6,18,19,20]</code>)
  </td>
  <td class='break-word'><span>only set this if you know what you're doing
</span></td>
</tr>


              
            </tbody>
          </table>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>></code>:
        returns object of results

      
    
  

  

  

  
    <div class='py1 quiet mt1 prose-big'>Example</div>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-comment">// get first, default property from device</span>
tuya.refresh().then(<span class="hljs-function"><span class="hljs-params">status</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(status))</pre>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-comment">// get second property from device</span>
tuya.refresh({<span class="hljs-attr">dps</span>: <span class="hljs-number">2</span>}).then(<span class="hljs-function"><span class="hljs-params">status</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(status))</pre>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-comment">// get all available data from device</span>
tuya.refresh({<span class="hljs-attr">schema</span>: <span class="hljs-literal">true</span>}).then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(data))</pre>
    
  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='tuyadeviceset'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>set(options)</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Sets a property on a device.</p>

    <div class='pre p1 fill-light mt0'>set(options: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>></div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>options</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)</code>
	    
          </div>
          
          <table class='mt1 mb2 fixed-table h5 col-12'>
            <colgroup>
              <col width='30%' />
              <col width='70%' />
            </colgroup>
            <thead>
              <tr class='bold fill-light'>
                <th>Name</th>
                <th>Description</th>
              </tr>
            </thead>
            <tbody class='mt1'>
              
                <tr>
  <td class='break-word'><span class='code bold'>options.dps</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></code>
  
    (default <code>1</code>)
  </td>
  <td class='break-word'><span>DPS index to set
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.set</span> <code class='quiet'>any?</code>
  </td>
  <td class='break-word'><span>value to set
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.cid</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>?</code>
  </td>
  <td class='break-word'><span>if specified, use device id of zigbee gateway and cid of subdevice to set its property
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.multiple</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>
  
    (default <code>false</code>)
  </td>
  <td class='break-word'><span>Whether or not multiple properties should be set with options.data
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.data</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a></code>
  
    (default <code>{}</code>)
  </td>
  <td class='break-word'><span>Multiple properties to set at once. See above.
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.shouldWaitForResponse</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>
  
    (default <code>true</code>)
  </td>
  <td class='break-word'><span>see

<a href="https://github.com/codetheweb/tuyapi/issues/420">#420</a>
 and

<a href="https://github.com/codetheweb/tuyapi/pull/421">#421</a>
 for details
</span></td>
</tr>


              
            </tbody>
          </table>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>></code>:
        returns response from device

      
    
  

  

  

  
    <div class='py1 quiet mt1 prose-big'>Example</div>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-comment">// set default property</span>
tuya.set({<span class="hljs-attr">set</span>: <span class="hljs-literal">true</span>}).then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'device was turned on'</span>))</pre>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-comment">// set custom property</span>
tuya.set({<span class="hljs-attr">dps</span>: <span class="hljs-number">2</span>, <span class="hljs-attr">set</span>: <span class="hljs-literal">false</span>}).then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'device was turned off'</span>))</pre>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-comment">// set multiple properties</span>
tuya.set({
          <span class="hljs-attr">multiple</span>: <span class="hljs-literal">true</span>,
          <span class="hljs-attr">data</span>: {
            <span class="hljs-string">'1'</span>: <span class="hljs-literal">true</span>,
            <span class="hljs-string">'2'</span>: <span class="hljs-string">'white'</span>
         }}).then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'device was changed'</span>))</pre>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-comment">// set custom property for a specific (virtual) deviceId</span>
tuya.set({
          <span class="hljs-attr">dps</span>: <span class="hljs-number">2</span>,
          <span class="hljs-attr">set</span>: <span class="hljs-literal">false</span>,
          <span class="hljs-attr">devId</span>: <span class="hljs-string">'04314116cc50e346566e'</span>
         }).then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'device was turned off'</span>))</pre>
    
  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='tuyadeviceconnect'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>connect()</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Connects to the device. Can be called even
if device is already connected.</p>

    <div class='pre p1 fill-light mt0'>connect(): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>></div>
  
  

  
  
  
  
  
  

  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>></code>:
        <code>true</code>
 if connect succeeds

      
    
  

  

  

  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='tuyadevicedisconnect'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>disconnect()</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Disconnects from the device, use to
close the socket and exit gracefully.</p>

    <div class='pre p1 fill-light mt0'>disconnect()</div>
  
  

  
  
  
  
  
  

  

  

  

  

  

  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='tuyadeviceisconnected'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>isConnected()</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Returns current connection status to device.</p>

    <div class='pre p1 fill-light mt0'>isConnected(): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></div>
  
  

  
  
  
  
  
  

  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>:
        (
<code>true</code>
 if connected, 
<code>false</code>
 otherwise.)

      
    
  

  

  

  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='tuyadeviceresolveid'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>resolveId(options)</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  
    <div class='pre p1 fill-light mt0'>resolveId(options: any)</div>
  
  

  <div>Deprecated: since v3.0.0. Will be removed in v4.0.0. Use find() instead.
</div>
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>options</span> <code class='quiet'>(any)</code>
	    
          </div>
          
        </div>
      
    </div>
  

  

  

  

  

  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='tuyadevicefind'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>find(options = {})</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Finds an ID or IP, depending on what's missing.
If you didn't pass an ID or IP to the constructor,
you must call this before anything else.</p>

    <div class='pre p1 fill-light mt0'>find(options: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>?): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a>)></div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>options</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>?
            = <code>{}</code>)</code>
	    
          </div>
          
          <table class='mt1 mb2 fixed-table h5 col-12'>
            <colgroup>
              <col width='30%' />
              <col width='70%' />
            </colgroup>
            <thead>
              <tr class='bold fill-light'>
                <th>Name</th>
                <th>Description</th>
              </tr>
            </thead>
            <tbody class='mt1'>
              
                <tr>
  <td class='break-word'><span class='code bold'>options.timeout</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></code>
  
    (default <code>10</code>)
  </td>
  <td class='break-word'><span>how long, in seconds, to wait for device
to be resolved before timeout error is thrown
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.all</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>?</code>
  
    (default <code>false</code>)
  </td>
  <td class='break-word'><span>true to return array of all found devices
</span></td>
</tr>


              
            </tbody>
          </table>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a>)></code>:
        true if ID/IP was found and device is ready to be used

      
    
  

  

  

  
    <div class='py1 quiet mt1 prose-big'>Example</div>
    
      
      <pre class='p1 overflow-auto round fill-light'>tuya.find().then(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'ready!'</span>))</pre>
    
  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='tuyadevicetoggle'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>toggle(property)</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Toggles a boolean property.</p>

    <div class='pre p1 fill-light mt0'>toggle(property: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a>): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>></div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>property</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a>
            = <code>1</code>)</code>
	    property to toggle

          </div>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise">Promise</a>&#x3C;<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>></code>:
        the resulting state

      
    
  

  

  

  

  

  

  

  
</section>

      </div>
    </div>
  
</div>

  

  

  
</section>

          
        
          
            <div class='keyline-top-not py2'><section class='py2 clearfix'>

  <h2 id='events' class='mt0'>
    Events
  </h2>

  
    <p>Events that TuyAPI emits.</p>

  
</section></div>
          
        
          
          <section class='p2 mb2 clearfix bg-white minishadow'>

  
  <div class='clearfix'>
    
    <h3 class='fl m0' id='eventtuyadeviceerror'>
      TuyaDevice#error
    </h3>
    
    
  </div>
  

  <p>Emitted on socket error, usually a
result of a connection timeout.
Also emitted on parsing errors.</p>

    <div class='pre p1 fill-light mt0'>TuyaDevice#error</div>
  
  

  
  
  
  
  
  

  

  
    <div class='py1 quiet mt1 prose-big'>Properties</div>
    <div>
      
        <div class='space-bottom0'>
          <span class='code bold'>error</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error">Error</a>)</code>
          : error event

          
        </div>
      
    </div>
  

  

  

  

  

  

  

  

  
</section>

          
        
          
          <section class='p2 mb2 clearfix bg-white minishadow'>

  
  <div class='clearfix'>
    
    <h3 class='fl m0' id='eventtuyadeviceconnected'>
      TuyaDevice#connected
    </h3>
    
    
  </div>
  

  <p>Emitted when socket is connected
to device. This event may be emitted
multiple times within the same script,
so don't use this as a trigger for your
initialization code.</p>

    <div class='pre p1 fill-light mt0'>TuyaDevice#connected</div>
  
  

  
  
  
  
  
  

  

  

  

  

  

  

  

  

  

  
</section>

          
        
          
          <section class='p2 mb2 clearfix bg-white minishadow'>

  
  <div class='clearfix'>
    
    <h3 class='fl m0' id='eventtuyadeviceheartbeat'>
      TuyaDevice#heartbeat
    </h3>
    
    
  </div>
  

  <p>Emitted when a heartbeat ping is returned.</p>

    <div class='pre p1 fill-light mt0'>TuyaDevice#heartbeat</div>
  
  

  
  
  
  
  
  

  

  

  

  

  

  

  

  

  

  
</section>

          
        
          
          <section class='p2 mb2 clearfix bg-white minishadow'>

  
  <div class='clearfix'>
    
    <h3 class='fl m0' id='eventtuyadevicedp-refresh'>
      TuyaDevice#dp-refresh
    </h3>
    
    
  </div>
  

  <p>Emitted when dp_refresh data is proactive returned from device, omitting dps 1
Only changed dps are returned.</p>

    <div class='pre p1 fill-light mt0'>TuyaDevice#dp-refresh</div>
  
  

  
  
  
  
  
  

  

  
    <div class='py1 quiet mt1 prose-big'>Properties</div>
    <div>
      
        <div class='space-bottom0'>
          <span class='code bold'>data</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)</code>
          : received data

          
        </div>
      
        <div class='space-bottom0'>
          <span class='code bold'>commandByte</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a>)</code>
          : commandByte of result( 8=proactive update from device)

          
        </div>
      
        <div class='space-bottom0'>
          <span class='code bold'>sequenceN</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a>)</code>
          : the packet sequence number

          
        </div>
      
    </div>
  

  

  

  

  

  

  

  

  
</section>

          
        
          
          <section class='p2 mb2 clearfix bg-white minishadow'>

  
  <div class='clearfix'>
    
    <h3 class='fl m0' id='eventtuyadevicedata'>
      TuyaDevice#data
    </h3>
    
    
  </div>
  

  <p>Emitted when data is returned from device.</p>

    <div class='pre p1 fill-light mt0'>TuyaDevice#data</div>
  
  

  
  
  
  
  
  

  

  
    <div class='py1 quiet mt1 prose-big'>Properties</div>
    <div>
      
        <div class='space-bottom0'>
          <span class='code bold'>data</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)</code>
          : received data

          
        </div>
      
        <div class='space-bottom0'>
          <span class='code bold'>commandByte</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a>)</code>
          : commandByte of result
(e.g. 7=requested response, 8=proactive update from device)

          
        </div>
      
        <div class='space-bottom0'>
          <span class='code bold'>sequenceN</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a>)</code>
          : the packet sequence number

          
        </div>
      
    </div>
  

  

  

  

  

  

  

  

  
</section>

          
        
          
          <section class='p2 mb2 clearfix bg-white minishadow'>

  
  <div class='clearfix'>
    
    <h3 class='fl m0' id='eventtuyadevicedisconnected'>
      TuyaDevice#disconnected
    </h3>
    
    
  </div>
  

  <p>Emitted when a socket is disconnected
from device. Not an exclusive event:
<code>error</code> and <code>disconnected</code> may be emitted
at the same time if, for example, the device
goes off the network.</p>

    <div class='pre p1 fill-light mt0'>TuyaDevice#disconnected</div>
  
  

  
  
  
  
  
  

  

  

  

  

  

  

  

  

  

  
</section>

          
        
          
          <section class='p2 mb2 clearfix bg-white minishadow'>

  
  <div class='clearfix'>
    
    <h3 class='fl m0' id='packet'>
      Packet
    </h3>
    
    
  </div>
  

  <p>A complete packet.</p>

    <div class='pre p1 fill-light mt0'>Packet</div>
  
    <p>
      Type:
      <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>
    </p>
  
  

  
  
  
  
  
  

  

  
    <div class='py1 quiet mt1 prose-big'>Properties</div>
    <div>
      
        <div class='space-bottom0'>
          <span class='code bold'>payload</span> <code class='quiet'>((<a href="https://nodejs.org/api/buffer.html">Buffer</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>))</code>
          : Buffer if hasn't been decoded, object or
string if it has been

          
        </div>
      
        <div class='space-bottom0'>
          <span class='code bold'>leftover</span> <code class='quiet'>(<a href="https://nodejs.org/api/buffer.html">Buffer</a>)</code>
          : bytes adjacent to the parsed packet

          
        </div>
      
        <div class='space-bottom0'>
          <span class='code bold'>commandByte</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a>)</code>
          
          
        </div>
      
        <div class='space-bottom0'>
          <span class='code bold'>sequenceN</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a>)</code>
          
          
        </div>
      
    </div>
  

  

  

  

  

  

  

  

  
</section>

          
        
          
          <section class='p2 mb2 clearfix bg-white minishadow'>

  
  <div class='clearfix'>
    
    <h3 class='fl m0' id='messageparser'>
      MessageParser
    </h3>
    
    
  </div>
  

  <p>Low-level class for parsing packets.</p>

    <div class='pre p1 fill-light mt0'>new MessageParser(options: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)</div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>options</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>
            = <code>{}</code>)</code>
	    
          </div>
          
          <table class='mt1 mb2 fixed-table h5 col-12'>
            <colgroup>
              <col width='30%' />
              <col width='70%' />
            </colgroup>
            <thead>
              <tr class='bold fill-light'>
                <th>Name</th>
                <th>Description</th>
              </tr>
            </thead>
            <tbody class='mt1'>
              
                <tr>
  <td class='break-word'><span class='code bold'>options.key</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a></code>
  </td>
  <td class='break-word'><span>localKey of cipher
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.version</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></code>
  
    (default <code>3.1</code>)
  </td>
  <td class='break-word'><span>protocol version
</span></td>
</tr>


              
            </tbody>
          </table>
          
        </div>
      
    </div>
  

  

  

  

  

  
    <div class='py1 quiet mt1 prose-big'>Example</div>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-keyword">const</span> parser = <span class="hljs-keyword">new</span> MessageParser({<span class="hljs-attr">key</span>: <span class="hljs-string">'xxxxxxxxxxxxxxxx'</span>, <span class="hljs-attr">version</span>: <span class="hljs-number">3.1</span>})</pre>
    
  

  

  
    <div class='py1 quiet mt1 prose-big'>Instance Members</div>
    <div class="clearfix">
  
    <div class='border-bottom' id='messageparserparsepacket'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>parsePacket(buffer)</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Parses a Buffer of data containing at least
one complete packet at the begining of the buffer.
Will return multiple packets if necessary.</p>

    <div class='pre p1 fill-light mt0'>parsePacket(buffer: <a href="https://nodejs.org/api/buffer.html">Buffer</a>): <a href="#packet">Packet</a></div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>buffer</span> <code class='quiet'>(<a href="https://nodejs.org/api/buffer.html">Buffer</a>)</code>
	    of data to parse

          </div>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="#packet">Packet</a></code>:
        packet of data

      
    
  

  

  

  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='messageparsergetpayload'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>getPayload(data)</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Attempts to decode a given payload into
an object or string.</p>

    <div class='pre p1 fill-light mt0'>getPayload(data: <a href="https://nodejs.org/api/buffer.html">Buffer</a>): (<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>)</div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>data</span> <code class='quiet'>(<a href="https://nodejs.org/api/buffer.html">Buffer</a>)</code>
	    to decode

          </div>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>)</code>:
        object if payload is JSON, otherwise string

      
    
  

  

  

  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='messageparserparse'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>parse(buffer)</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Given a buffer potentially containing
multiple packets, this parses and returns
all of them.</p>

    <div class='pre p1 fill-light mt0'>parse(buffer: <a href="https://nodejs.org/api/buffer.html">Buffer</a>): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a>&#x3C;<a href="#packet">Packet</a>></div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>buffer</span> <code class='quiet'>(<a href="https://nodejs.org/api/buffer.html">Buffer</a>)</code>
	    to parse

          </div>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array">Array</a>&#x3C;<a href="#packet">Packet</a>></code>:
        parsed packets

      
    
  

  

  

  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='messageparserencode'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>encode(options)</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Encodes a payload into a
Tuya-protocol-complient packet.</p>

    <div class='pre p1 fill-light mt0'>encode(options: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>): <a href="https://nodejs.org/api/buffer.html">Buffer</a></div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>options</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)</code>
	    
          </div>
          
          <table class='mt1 mb2 fixed-table h5 col-12'>
            <colgroup>
              <col width='30%' />
              <col width='70%' />
            </colgroup>
            <thead>
              <tr class='bold fill-light'>
                <th>Name</th>
                <th>Description</th>
              </tr>
            </thead>
            <tbody class='mt1'>
              
                <tr>
  <td class='break-word'><span class='code bold'>options.data</span> <code class='quiet'>(<a href="https://nodejs.org/api/buffer.html">Buffer</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)</code>
  </td>
  <td class='break-word'><span>data to encode
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.encrypted</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>
  </td>
  <td class='break-word'><span>whether or not to encrypt the data
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.commandByte</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></code>
  </td>
  <td class='break-word'><span>command byte of packet (use CommandType definitions)
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.sequenceN</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a>?</code>
  </td>
  <td class='break-word'><span>optional, sequence number
</span></td>
</tr>


              
            </tbody>
          </table>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="https://nodejs.org/api/buffer.html">Buffer</a></code>:
        

      
    
  

  

  

  

  

  

  

  
</section>

      </div>
    </div>
  
</div>

  

  

  
</section>

          
        
          
          <section class='p2 mb2 clearfix bg-white minishadow'>

  
  <div class='clearfix'>
    
    <h3 class='fl m0' id='tuyacipher'>
      TuyaCipher
    </h3>
    
    
  </div>
  

  <p>Low-level class for encrypting and decrypting payloads.</p>

    <div class='pre p1 fill-light mt0'>new TuyaCipher(options: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)</div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>options</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)</code>
	    
          </div>
          
          <table class='mt1 mb2 fixed-table h5 col-12'>
            <colgroup>
              <col width='30%' />
              <col width='70%' />
            </colgroup>
            <thead>
              <tr class='bold fill-light'>
                <th>Name</th>
                <th>Description</th>
              </tr>
            </thead>
            <tbody class='mt1'>
              
                <tr>
  <td class='break-word'><span class='code bold'>options.key</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a></code>
  </td>
  <td class='break-word'><span>localKey of cipher
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.version</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></code>
  </td>
  <td class='break-word'><span>protocol version
</span></td>
</tr>


              
            </tbody>
          </table>
          
        </div>
      
    </div>
  

  

  

  

  

  
    <div class='py1 quiet mt1 prose-big'>Example</div>
    
      
      <pre class='p1 overflow-auto round fill-light'><span class="hljs-keyword">const</span> cipher = <span class="hljs-keyword">new</span> TuyaCipher({<span class="hljs-attr">key</span>: <span class="hljs-string">'xxxxxxxxxxxxxxxx'</span>, <span class="hljs-attr">version</span>: <span class="hljs-number">3.1</span>})</pre>
    
  

  

  
    <div class='py1 quiet mt1 prose-big'>Instance Members</div>
    <div class="clearfix">
  
    <div class='border-bottom' id='tuyacipherencrypt'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>encrypt(options)</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Encrypts data.</p>

    <div class='pre p1 fill-light mt0'>encrypt(options: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>): (<a href="https://nodejs.org/api/buffer.html">Buffer</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>)</div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>options</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a>)</code>
	    
          </div>
          
          <table class='mt1 mb2 fixed-table h5 col-12'>
            <colgroup>
              <col width='30%' />
              <col width='70%' />
            </colgroup>
            <thead>
              <tr class='bold fill-light'>
                <th>Name</th>
                <th>Description</th>
              </tr>
            </thead>
            <tbody class='mt1'>
              
                <tr>
  <td class='break-word'><span class='code bold'>options.data</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a></code>
  </td>
  <td class='break-word'><span>data to encrypt
</span></td>
</tr>


              
                <tr>
  <td class='break-word'><span class='code bold'>options.base64</span> <code class='quiet'><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean">Boolean</a></code>
  
    (default <code>true</code>)
  </td>
  <td class='break-word'><span><code>true</code>
 to return result in Base64
</span></td>
</tr>


              
            </tbody>
          </table>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code>(<a href="https://nodejs.org/api/buffer.html">Buffer</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>)</code>:
        returns Buffer unless options.base64 is true

      
    
  

  

  

  
    <div class='py1 quiet mt1 prose-big'>Example</div>
    
      
      <pre class='p1 overflow-auto round fill-light'>TuyaCipher.encrypt({<span class="hljs-attr">data</span>: <span class="hljs-string">'hello world'</span>})</pre>
    
  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='tuyacipherdecrypt'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>decrypt(data)</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Decrypts data.</p>

    <div class='pre p1 fill-light mt0'>decrypt(data: (<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a> | <a href="https://nodejs.org/api/buffer.html">Buffer</a>)): (<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>)</div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>data</span> <code class='quiet'>((<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a> | <a href="https://nodejs.org/api/buffer.html">Buffer</a>))</code>
	    to decrypt

          </div>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object">Object</a> | <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>)</code>:
        returns object if data is JSON, else returns string

      
    
  

  

  

  

  

  

  

  
</section>

      </div>
    </div>
  
    <div class='border-bottom' id='tuyaciphermd5'>
      <div class="clearfix small pointer toggle-sibling">
        <div class="py1 contain">
            <a class='icon pin-right py1 dark-link caret-right'>▸</a>
            <span class='code strong strong truncate'>md5(data)</span>
        </div>
      </div>
      <div class="clearfix display-none toggle-target">
        <section class='p2 mb2 clearfix bg-white minishadow'>

  

  <p>Calculates a MD5 hash.</p>

    <div class='pre p1 fill-light mt0'>md5(data: <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a></div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>data</span> <code class='quiet'>(<a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a>)</code>
	    to hash

          </div>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String">String</a></code>:
        characters 8 through 16 of hash of data

      
    
  

  

  

  

  

  

  

  
</section>

      </div>
    </div>
  
</div>

  

  

  
</section>

          
        
          
          <section class='p2 mb2 clearfix bg-white minishadow'>

  
  <div class='clearfix'>
    
    <h3 class='fl m0' id='crc32'>
      crc32
    </h3>
    
    
  </div>
  

  <p>Computes a Tuya flavored CRC32</p>

    <div class='pre p1 fill-light mt0'>crc32(bytes: Iterable): <a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></div>
  
  

  
  
  
  
  
  

  
    <div class='py1 quiet mt1 prose-big'>Parameters</div>
    <div class='prose'>
      
        <div class='space-bottom0'>
          <div>
            <span class='code bold'>bytes</span> <code class='quiet'>(Iterable)</code>
	    
          </div>
          
        </div>
      
    </div>
  

  

  
    
      <div class='py1 quiet mt1 prose-big'>Returns</div>
      <code><a href="https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number">Number</a></code>:
        Tuya CRC32

      
    
  

  

  

  

  

  

  

  
</section>

          
        
      </div>
    </div>
  <script src='assets/anchor.js'></script>
  <script src='assets/split.js'></script>
  <script src='assets/site.js'></script>
</body>
</html>
